package com.sfeir.client;

import static com.sfeir.client.util.Constants.KEY_AUDIO_DOWN;
import static com.sfeir.client.util.Constants.KEY_AUDIO_FALL;
import static com.sfeir.client.util.Constants.KEY_AUDIO_HARP;
import static com.sfeir.client.util.Constants.KEY_AUDIO_ROTATE;
import static com.sfeir.client.util.Constants.KEY_IMAGE_ABOUT;
import static com.sfeir.client.util.Constants.KEY_IMAGE_BACK;
import static com.sfeir.client.util.Constants.KEY_IMAGE_BACKGROUND;
import static com.sfeir.client.util.Constants.KEY_IMAGE_BAR;
import static com.sfeir.client.util.Constants.KEY_IMAGE_BAR_PART;
import static com.sfeir.client.util.Constants.KEY_IMAGE_HOW_TO;
import static com.sfeir.client.util.Constants.KEY_IMAGE_IL;
import static com.sfeir.client.util.Constants.KEY_IMAGE_IL_PART;
import static com.sfeir.client.util.Constants.KEY_IMAGE_IS;
import static com.sfeir.client.util.Constants.KEY_IMAGE_IS_PART;
import static com.sfeir.client.util.Constants.KEY_IMAGE_KEY_BLANK;
import static com.sfeir.client.util.Constants.KEY_IMAGE_KEY_SPACE;
import static com.sfeir.client.util.Constants.KEY_IMAGE_L;
import static com.sfeir.client.util.Constants.KEY_IMAGE_LEVEL_0;
import static com.sfeir.client.util.Constants.KEY_IMAGE_LEVEL_1;
import static com.sfeir.client.util.Constants.KEY_IMAGE_LEVEL_10;
import static com.sfeir.client.util.Constants.KEY_IMAGE_LEVEL_2;
import static com.sfeir.client.util.Constants.KEY_IMAGE_LEVEL_3;
import static com.sfeir.client.util.Constants.KEY_IMAGE_LEVEL_4;
import static com.sfeir.client.util.Constants.KEY_IMAGE_LEVEL_5;
import static com.sfeir.client.util.Constants.KEY_IMAGE_LEVEL_6;
import static com.sfeir.client.util.Constants.KEY_IMAGE_LEVEL_7;
import static com.sfeir.client.util.Constants.KEY_IMAGE_LEVEL_8;
import static com.sfeir.client.util.Constants.KEY_IMAGE_LEVEL_9;
import static com.sfeir.client.util.Constants.KEY_IMAGE_LOCK;
import static com.sfeir.client.util.Constants.KEY_IMAGE_LOGO_SFEIR;
import static com.sfeir.client.util.Constants.KEY_IMAGE_L_PART;
import static com.sfeir.client.util.Constants.KEY_IMAGE_NEXT;
import static com.sfeir.client.util.Constants.KEY_IMAGE_PANEL;
import static com.sfeir.client.util.Constants.KEY_IMAGE_PLAY;
import static com.sfeir.client.util.Constants.KEY_IMAGE_REFRESH;
import static com.sfeir.client.util.Constants.KEY_IMAGE_S;
import static com.sfeir.client.util.Constants.KEY_IMAGE_SELECTION;
import static com.sfeir.client.util.Constants.KEY_IMAGE_SETTINGS;
import static com.sfeir.client.util.Constants.KEY_IMAGE_SOUND_OFF;
import static com.sfeir.client.util.Constants.KEY_IMAGE_SOUND_ON;
import static com.sfeir.client.util.Constants.KEY_IMAGE_SQUARE;
import static com.sfeir.client.util.Constants.KEY_IMAGE_SQUARE_PART;
import static com.sfeir.client.util.Constants.KEY_IMAGE_STAR;
import static com.sfeir.client.util.Constants.KEY_IMAGE_STAR_EMPTY;
import static com.sfeir.client.util.Constants.KEY_IMAGE_STAR_SMALL;
import static com.sfeir.client.util.Constants.KEY_IMAGE_S_PART;
import static com.sfeir.client.util.Constants.KEY_IMAGE_T;
import static com.sfeir.client.util.Constants.KEY_IMAGE_TETRIS_LOGO;
import static com.sfeir.client.util.Constants.KEY_IMAGE_TWITTER;
import static com.sfeir.client.util.Constants.KEY_IMAGE_T_PART;
import static com.sfeir.client.util.Constants.KEY_IMAGE_WINNER;

import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.media.client.Audio;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.web.bindery.event.shared.EventBus;
import com.google.web.bindery.event.shared.SimpleEventBus;
import com.sfeir.canvas.activities.client.CanvasActivityManager;
import com.sfeir.canvas.activities.client.CanvasNavigationManager;
import com.sfeir.canvas.activities.client.CanvasView;
import com.sfeir.canvas.util.client.ResourceManager;
import com.sfeir.client.activity.CreditActivity;
import com.sfeir.client.activity.GameOverActivity;
import com.sfeir.client.activity.GamePanelActivity;
import com.sfeir.client.activity.HowToActivity;
import com.sfeir.client.activity.LevelPickerActivity;
import com.sfeir.client.activity.LoadingActivity;
import com.sfeir.client.activity.MainGameActivity;
import com.sfeir.client.activity.SettingsActivity;
import com.sfeir.client.service.AppServiceAsync;
import com.sfeir.client.service.CachedAppServiceAsync;
import com.sfeir.client.view.ConnectedUserView;
import com.sfeir.client.view.ConnectedUserViewImpl;
import com.sfeir.client.view.ErrorWidget;
import com.sfeir.client.view.GamePanelViewImpl;
import static com.sfeir.canvas.activities.client.event.PageChangeEvent.*;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Game implements EntryPoint {
	private static final String SELECTION = "selection";
	private static final String CREDITS = "credits";
	private static final String HOWTO = "howto";
	private static final String GAME = "game";
	private static final String TITLE = "title";
	private static final String GAME_OVER = "gameover";
	private static final String SETTINGS = "settings";
	private static final String ABOUT = "about";
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		final RootPanel rootPanel = RootPanel.get();
		if (!Canvas.isSupported()) {
			RootPanel.get("content").setStyleName("hidden");
			rootPanel.add(new ErrorWidget());
			return;
		}
		if (!Audio.isSupported()) {
			Window.alert("Audio is desactivated since your browser doesn't support it.");
		}
		final Panel panel = RootPanel.get("dummy");
		EventBus bus = new SimpleEventBus();
		ResourceManager resourceManager = new ResourceManager(panel, bus);
		registerResources(resourceManager);

		AppServiceAsync service = new CachedAppServiceAsync();
		CanvasView view = new GamePanelViewImpl();
		CanvasNavigationManager nav = new CanvasNavigationManager();
		registerNavigation(nav);
		
		CanvasActivityManager manager = new CanvasActivityManager(bus, nav, view);
		registerActivities(manager, resourceManager, service);
		RootPanel.get("content").add(view);
		manager.start();
		service.getNickname(new AsyncCallback<String>() {

			@Override
			public void onSuccess(String result) {
				if (result != null && !"".equals(result)) {
					ConnectedUserView connectView = new ConnectedUserViewImpl();
					connectView.setText(result);
					rootPanel.add(connectView);
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Unable to retrieve conntected user, cause : " + caught.getMessage());
			}
		});
	}

	private void registerActivities(CanvasActivityManager manager, ResourceManager resourceManager,
			AppServiceAsync service) {
		manager.registerActivity(GAME, new GamePanelActivity(resourceManager, service));
		manager.registerActivity(SELECTION, new LevelPickerActivity(resourceManager, service));
		manager.registerActivity(TITLE, new MainGameActivity(resourceManager));
		manager.registerActivity(GAME_OVER, new GameOverActivity(resourceManager, service));
		manager.registerActivity(CREDITS, new CreditActivity(resourceManager));
		manager.registerActivity(HOWTO, new HowToActivity(resourceManager));
		manager.registerActivity(SETTINGS, new SettingsActivity(resourceManager));
		manager.setDefaultActivity(new LoadingActivity(resourceManager, service));
	}
	
	private void registerNavigation(CanvasNavigationManager nav) {
		nav.registerNavigation("default", NEXT, TITLE);
		nav.registerNavigation(TITLE, NEXT, SELECTION);
		nav.registerNavigation(TITLE, HOWTO, HOWTO);
		nav.registerNavigation(TITLE, ABOUT, CREDITS);
		nav.registerNavigation(TITLE, SETTINGS, SETTINGS);
		nav.registerNavigation(SELECTION, NEXT, GAME);
		nav.registerNavigation(SELECTION, BACK, TITLE);
		nav.registerNavigation(HOWTO, BACK, TITLE);
		nav.registerNavigation(CREDITS, BACK, TITLE);
		nav.registerNavigation(SETTINGS, BACK, TITLE);
		nav.registerNavigation(GAME, NEXT, GAME_OVER);
		nav.registerNavigation(GAME, BACK, SELECTION);
		nav.registerNavigation(GAME_OVER, NEXT, GAME);
		nav.registerNavigation(GAME_OVER, BACK, SELECTION);
	}

	private void registerResources(ResourceManager resourceManager) {
		resourceManager.registerImage(KEY_IMAGE_BACK, "game/back.png", 3);
		resourceManager.registerImage(KEY_IMAGE_BACKGROUND, "game/background.png", 500);// 1192);
		resourceManager.registerImage(KEY_IMAGE_NEXT, "game/next.png", 2);
		resourceManager.registerImage(KEY_IMAGE_PLAY, "game/start.png", 15);
		resourceManager.registerImage(KEY_IMAGE_REFRESH, "game/refresh.png", 5);
		resourceManager.registerImage(KEY_IMAGE_PANEL, "game/panel.png", 12);
		resourceManager.registerImage(KEY_IMAGE_STAR, "game/star-big.png", 4);
		resourceManager.registerImage(KEY_IMAGE_STAR_SMALL, "game/star-small.png", 2);
		resourceManager.registerImage(KEY_IMAGE_STAR_EMPTY, "game/star_empty.png", 2);
		resourceManager.registerImage(KEY_IMAGE_LOCK, "game/lock.png", 12);
		resourceManager.registerImage(KEY_IMAGE_HOW_TO, "game/howto.png", 8);
		resourceManager.registerImage(KEY_IMAGE_ABOUT, "game/about.png", 5);
		resourceManager.registerImage(KEY_IMAGE_TETRIS_LOGO, "game/tetrislogo.png", 12);
		resourceManager.registerImage(KEY_IMAGE_WINNER, "game/winner.jpg", 12);
		resourceManager.registerImage(KEY_IMAGE_TWITTER, "game/twitter.png", 1);
		resourceManager.registerImage(KEY_IMAGE_SELECTION, "game/selection-img.png", 1);
		resourceManager.registerImage(KEY_IMAGE_SETTINGS, "game/settings.png", 5);
		resourceManager.registerImage(KEY_IMAGE_SOUND_ON, "game/soundon.png", 2);
		resourceManager.registerImage(KEY_IMAGE_SOUND_OFF, "game/soundoff.png", 2);

		resourceManager.registerImage(KEY_IMAGE_KEY_BLANK, "game/keyboard-empty.png", 2);
		resourceManager.registerImage(KEY_IMAGE_KEY_SPACE, "game/keyboard-space.png", 4);

		resourceManager.registerImage(KEY_IMAGE_LOGO_SFEIR, "game/logo_sfeir.jpg", 12);

		resourceManager.registerImage(KEY_IMAGE_S_PART, "game/brick-blue.png", 2);
		resourceManager.registerImage(KEY_IMAGE_BAR_PART, "game/brick-gray.png", 2);
		resourceManager.registerImage(KEY_IMAGE_IL_PART, "game/brick-green.png", 2);
		resourceManager.registerImage(KEY_IMAGE_T_PART, "game/brick-lightblue.png", 2);
		resourceManager.registerImage(KEY_IMAGE_IS_PART, "game/brick-orange.png", 2);
		resourceManager.registerImage(KEY_IMAGE_L_PART, "game/brick-red.png", 2);
		resourceManager.registerImage(KEY_IMAGE_SQUARE_PART, "game/brick-yellow.png", 2);

		resourceManager.registerImage(KEY_IMAGE_S, "game/S.png", 2);
		resourceManager.registerImage(KEY_IMAGE_IS, "game/IS.png", 3);
		resourceManager.registerImage(KEY_IMAGE_L, "game/L.png", 3);
		resourceManager.registerImage(KEY_IMAGE_IL, "game/IL.png", 2);
		resourceManager.registerImage(KEY_IMAGE_T, "game/T.png", 2);
		resourceManager.registerImage(KEY_IMAGE_SQUARE, "game/SQUARE.png", 2);
		resourceManager.registerImage(KEY_IMAGE_BAR, "game/BAR.png", 2);

		resourceManager.registerImage(KEY_IMAGE_LEVEL_0, "game/level0.png", 1);
		resourceManager.registerImage(KEY_IMAGE_LEVEL_1, "game/level1.png", 1);
		resourceManager.registerImage(KEY_IMAGE_LEVEL_2, "game/level2.png", 2);
		resourceManager.registerImage(KEY_IMAGE_LEVEL_3, "game/level3.png", 2);
		resourceManager.registerImage(KEY_IMAGE_LEVEL_4, "game/level4.png", 1);
		resourceManager.registerImage(KEY_IMAGE_LEVEL_5, "game/level5.png", 2);
		resourceManager.registerImage(KEY_IMAGE_LEVEL_6, "game/level6.png", 2);
		resourceManager.registerImage(KEY_IMAGE_LEVEL_7, "game/level7.png", 1);
		resourceManager.registerImage(KEY_IMAGE_LEVEL_8, "game/level8.png", 2);
		resourceManager.registerImage(KEY_IMAGE_LEVEL_9, "game/level9.png", 2);
		resourceManager.registerImage(KEY_IMAGE_LEVEL_10, "game/level10.png", 2);

		resourceManager.registerAudio(KEY_AUDIO_DOWN, "game/down.wav", 86);
		resourceManager.registerAudio(KEY_AUDIO_DOWN, "game/down.mp3", 32);
		resourceManager.registerAudio(KEY_AUDIO_HARP, "game/harp.mp3", 9);
		resourceManager.registerAudio(KEY_AUDIO_HARP, "game/harp.wav", 48);
		resourceManager.registerAudio(KEY_AUDIO_FALL, "game/fall.wav", 14);
		resourceManager.registerAudio(KEY_AUDIO_FALL, "game/fall.mp3", 6);
		resourceManager.registerAudio(KEY_AUDIO_ROTATE, "game/rotate.wav", 23);
		resourceManager.registerAudio(KEY_AUDIO_ROTATE, "game/rotate.mp3", 9);
	}
}
